﻿namespace LiteBlog.Infrastructure.Constraints
{
    using System.Web;
    using System.Web.Routing;

    public class ArchiveConstraint : IRouteConstraint
    {
        public ArchiveConstraint()
        {
        }

        public bool Match(
            HttpContextBase httpContext, 
            Route route, 
            string parameterName, 
            RouteValueDictionary values, 
            RouteDirection routeDirection)
        {
            try
            {
                if (parameterName == "year")
                {
                    int year = int.Parse(values["year"].ToString());
                    return (year > 1900 && year < 2100) ? true : false;
                }

                if (parameterName == "month")
                {
                    int month = int.Parse(values["month"].ToString());
                    return (month > 0 && month <= 12) ? true : false;
                }
            }
            catch
            {
                return false;
            }

            return true;
        }        
    }
}